package com.dzzw.Servlet.AdminServlet;

import com.dzzw.Dao.AdminDao;
import com.dzzw.Model.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/View/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {//管理员登录；

    private static final long serialVersionUID = 1L;
    private AdminDao adminDao = new AdminDao();
    public AdminLoginServlet() {
        super();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        /*Integer admin_id = Integer.valueOf(request.getParameter("admin_id"));*/
        /*Integer admin_id = Integer.parseInt("admin_id");*/
        String admin_actnumber = request.getParameter("admin_actnumber");
        String admin_password = request.getParameter("admin_password");
        String admin_name = request.getParameter("admin_name"); //将admin_name传到Admin.jsp页面上；
        /*Admin admin = new Admin(admin_id,admin_actnumber,admin_password,admin_name);*/
        Admin admin = adminDao.AdminLogin(admin_actnumber,admin_password,admin_name);
        if (admin != null) {
            HttpSession session = request.getSession();
            session.setAttribute("AdminLogin", admin);//传值
            response.sendRedirect("/View/Admin.jsp");
            System.out.println("管理员登录成功！");
        } else {
            response.sendRedirect("/View/Error404.jsp");
            System.out.println("管理员登录失败！");
        }
    }
}
